<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul class="list">
        <li></li>
    </ul>
    <button>获取电影</button>
    <button>添加电影</button>
    <button>修改电影</button>
    <button>局部修改PATCH大写</button>
    <button>删除电影</button>
    <script>
        // 获取ul和按钮
        let ul = document.querySelector('ul');
        let btns = document.querySelectorAll('button');

        //获取电影列表
        btns[0].onclick =async function () {
        //    console.log("123");
            // 获取电影列表
            let res =await fetch('http://api.zhibao.com/movies').then(val=>val.json());
            
            // console.log(res);
            ul.innerHTML = '';
            res.forEach(item => {
                ul.innerHTML += `<li>《${item.name}》导演：${item.director}；演员：${item.star}</li>`;
            });
        }

        // 添加电影 POST
        btns[1].onclick = function(){
            
            let p1 = fetch('http://api.zhibao.com/movies',{
                method:"POST", //设置请求方式为post
                headers:{
                    // "content-type":'application/x-www-form-urlencoded'
                    "content-type":'application/json'
                },
                body:JSON.stringify({
                    "name":"楚门的世界",
                    "director":"xxx",
                    "star":"彼得"
                })
                // 请求体数据
                // body:`name=我不是药神&director=文牧野&star=徐峥`
            });

            p1.then((val)=>{
                console.log("成功了",val);
            });

        }
    
        // 毁灭式 全部 修改电影 - put
        btns[2].onclick = function(){
            // 修改电影
            fetch('http://api.zhibao.com/movies/2',{
                method:"PUT",
                headers:{
                    "content-type":'application/json'
                },
                body:JSON.stringify({
                    "name":"阿凡达",
                    "director":"xxx",
                    "star":"啊啊啊"
                })
            });
        }

        // 局部修改
        btns[3].onclick = function(){
           fetch('http://api.zhibao.com/movies/6',{
               method:"PATCH",
               headers:{
                   "content-type":"application/x-www-form-urlencoded"
               },
               body:`star=王传君`
           }).then(val=>{
               console.log(val);
           });
        }
    
        // 删除电影
        btns[4].onclick = function(){
            fetch('http://api.zhibao.com/movies/7',{
                method:"DELETE"
            }).then(val=>{
                console.log(val);
            });
        }
    </script>
</body>

</html>